package cn.dengta.context;

import cn.dengta.common.util.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;

/**
 * 当上下文刷新时，同步更新 SpringUtil.applicationContext
 */
public interface ContextRefreshListener extends ApplicationListener<EnvironmentChangeEvent> {

	Logger LOGGER = LoggerFactory.getLogger(ContextRefreshListener.class);

	@Override
	default void onApplicationEvent(EnvironmentChangeEvent event) {
		final Object source = event.getSource();
		if (source instanceof ApplicationContext) {
			LOGGER.info("触发环境参数变更，刷新 ApplicationContext");
			SpringUtil.setGlobalApplicationContext((ApplicationContext) source);
		}
	}

}
